package com.cloud.drore.eboss.supplier.mapper.provider;


import com.cloud.drore.eboos.common.entity.supplier.Ticket;
import com.cloud.drore.eboos.common.entity.supplier.po.TicketQueryPo;
import com.cloud.drore.eboos.common.util.DateToStringUtil;
import org.apache.commons.lang.StringUtils;

/**
 * 浙江卓锐科技股份有限公司 版权所有  Copyright 2018<br>
 *
 * @Description: <br>
 * @Project: eboos <br>
 * @CreateDate: Created in 2018/1/8 10:41 <br>
 * @Author: <a href="575842778@qq.com">郑永涛</a>
 */
public class TicketProvider {

    public TicketProvider(){

    }
    private static String findTicketByParamSql = "select ticket.id as ticket_id,ticket.name,ticket.consume_id,ticket.crowd,ticket.stock_method,ticket.payment_type," +
            " ticket.instruction,ticket.is_send_message,ticket.common_product_id," +
            "ticket.refund_fee,ticket.refund_model,ticket.term_of_validity_start_time,ticket.term_of_validity_end_time,ticket.status," +
            "ticket.refund_one_day_ago,ticket.refund_two_days_ago,ticket.refund_three_days_ago,ticket.product_num,ticket.fixed_rate,ticket.is_real_name," +
            "ticket.reject_reason,ticket.common_product_id,price.sales_price,price.settlement_price,price.start_time,price.end_time " +
            " from eboos_product product " +
            "  INNER JOIN eboos_ticket ticket " +
            "  ON  product.product_id = ticket.id " +
            "  INNER JOIN eboos_product_consume_relation consumeRelation " +
            "  ON  product.id = consumeRelation.product_id " +
            "  LEFT JOIN eboos_ticket_price price " +
            "  ON product.product_id = price.ticket_id  " +
            " where ticket.is_deleted = 0 and product.product_type = 'TICKET_PRODUCT' ";

    private static String findTicketByParamDealerSql = "select ticket.id as ticket_id,ticket.name,ticket.consume_id,ticket.crowd,ticket.stock_method,ticket.payment_type," +
            " ticket.instruction,ticket.is_send_message,ticket.common_product_id," +
            "ticket.refund_fee,ticket.refund_model,ticket.term_of_validity_start_time,ticket.term_of_validity_end_time,ticket.status," +
            "ticket.refund_one_day_ago,ticket.refund_two_days_ago,ticket.refund_three_days_ago,ticket.product_num,ticket.fixed_rate,ticket.is_real_name," +
            "ticket.reject_reason,ticket.common_product_id,price.sales_price,price.settlement_price,price.start_time,price.end_time " +
            " from eboos_product product " +
            "  INNER JOIN eboos_ticket ticket " +
            "  ON  product.product_id = ticket.id " +
            "  INNER JOIN eboos_product_consume_relation consumeRelation " +
            "  ON  product.id = consumeRelation.product_id " +
            "  LEFT JOIN eboos_ticket_price price " +
            "  ON product.product_id = price.ticket_id " +
            "  INNER JOIN eboos_distributor_product_association disAsso " +
            "  ON  product.id = disAsso.main_id " +
            " where ticket.is_deleted = 0 and product.product_type = 'TICKET_PRODUCT' ";

    private static String selectTicketByIdSql = "select ticket.id as ticket_id,ticket.name,ticket.consume_id,ticket.crowd,ticket.stock_method,ticket.payment_type," +
            " ticket.instruction,ticket.is_send_message,ticket.common_product_id,ticket.product_code," +
            "ticket.refund_fee,ticket.refund_model,ticket.term_of_validity_start_time,ticket.term_of_validity_end_time,ticket.status," +
            "IFNULL(ticket.refund_one_day_ago,0) AS refund_one_day_ago,IFNULL(ticket.refund_two_days_ago,0) AS refund_two_days_ago," +
            "IFNULL(ticket.refund_three_days_ago,0) AS refund_three_days_ago,IFNULL(ticket.fixed_rate,0) AS fixed_rate," +
            "ticket.product_num,ticket.is_real_name, " +
            "price.sales_price,price.settlement_price,price.start_time,price.end_time " +
            " from eboos_ticket ticket " +
            " left join eboos_ticket_price price on ticket.id = price.ticket_id " +
            "  where ticket.is_deleted = 0 " +
            " and ticket.id = ";

    private static String findTicketListWithoutExpiredSql = "select ticket.id as ticket_id,ticket.name,ticket.consume_id,ticket.crowd,ticket.stock_method,ticket.payment_type," +
            " ticket.instruction,ticket.is_send_message,ticket.common_product_id," +
            "ticket.refund_fee,ticket.refund_model,ticket.term_of_validity_start_time,ticket.term_of_validity_end_time,ticket.status," +
            "ticket.refund_one_day_ago,ticket.refund_two_days_ago,ticket.refund_three_days_ago,ticket.product_num,ticket.fixed_rate,ticket.is_real_name," +
            "ticket.reject_reason,ticket.common_product_id,price.sales_price,price.settlement_price,price.start_time,price.end_time " +
            " from eboos_product product " +
            "  INNER JOIN eboos_ticket ticket " +
            "  ON  product.product_id = ticket.id " +
            "  INNER JOIN eboos_product_consume_relation consumeRelation " +
            "  ON  product.id = consumeRelation.product_id " +
            "  LEFT JOIN eboos_ticket_price price " +
            "  ON product.product_id = price.ticket_id  " +
            " where ticket.is_deleted = 0 and product.product_type = 'TICKET_PRODUCT' " +
            " and product.status != 'EXPIRED' ";

    public String findTicketByParam(TicketQueryPo ticket){
        String sql =findTicketByParamSql;

        if(StringUtils.isNotBlank(ticket.getName()))
            sql += " and ticket.name like '%" + ticket.getName() + "%'";
        if(ticket.getSupplierId() != null)
            sql += " and product.merchant_id='" + ticket.getSupplierId() + "'";
        if(StringUtils.isNotBlank(ticket.getStatus()))
            sql += " and ticket.status='" + ticket.getStatus() + "'";
        if(ticket.getPoiId()!=null)
            sql += " and consumeRelation.poi_id ='" + ticket.getPoiId() + "'";
        return sql;
    }


    public String findTicketListWithoutExpired(TicketQueryPo ticket){
        String sql =findTicketListWithoutExpiredSql;
        if(ticket.getSupplierId() != null)
            sql += " and product.merchant_id='" + ticket.getSupplierId() + "'";
        if(StringUtils.isNotBlank(ticket.getName()))
            sql += " and ticket.name like '%" + ticket.getName() + "%'";
        if(StringUtils.isNotBlank(ticket.getStatus()))
            sql += " and ticket.status='" + ticket.getStatus() + "'";
        if(ticket.getPoiId()!=null)
            sql += " and consumeRelation.poi_id ='" + ticket.getPoiId() + "'";
        return sql;
    }


    public String selectTicketById(Long ticketId){
        String sql = selectTicketByIdSql+" '"+ticketId+"' ";
        return sql;
    }


    public String selectTicketByProNum(String proNumber){
        String sql = "select ticket.*,price.* " +
                " from eboos_ticket ticket " +
                " left join eboos_ticket_price price on ticket.id = price.ticket_id " +
                "  where ticket.is_deleted = 0 " +
                " and ticket.product_num = '"+proNumber+"' ";
        return sql;
    }


    public String updateTicketByParam(Ticket ticket){
        int count = 0;
        String sql = " update eboos_ticket set ";
        if (StringUtils.isNotBlank(ticket.getName())) {
            sql += " name='" + ticket.getName() + "'";
            count++;
        }
        if (StringUtils.isNotBlank(ticket.getInstruction())) {
            if (count != 0) {
                sql += ",";
            }
            sql += " instruction='" + ticket.getInstruction() + "'";
            count++;
        }
        if (StringUtils.isNotBlank(ticket.getStockMethod())) {
            if (count != 0) {
                sql += ",";
            }
            sql += " stock_method ='" + ticket.getStockMethod() + "'";
            count++;
        }
        if (StringUtils.isNotBlank(ticket.getRefundModel())) {
            if (count != 0) {
                sql += ",";
            }
            sql += " refund_model ='" + ticket.getRefundModel() + "'";
            count++;
        }
        if (StringUtils.isNotBlank(ticket.getRefundFee())) {
            if (count != 0) {
                sql += ",";
            }
            sql += " refund_fee ='" + ticket.getRefundFee() + "'";
            count++;
        }
        if (ticket.getRefundThreeDaysAgo()!=null) {
            if (count != 0) {
                sql += ",";
            }
            sql += " refund_three_days_ago =" + ticket.getRefundThreeDaysAgo() + "";
            count++;
        }
        if (ticket.getRefundOneDayAgo()!=null) {
            if (count != 0) {
                sql += ",";
            }
            sql += " refund_one_day_ago =" + ticket.getRefundOneDayAgo() + "";
            count++;
        }
        if (ticket.getRefundThreeDaysAgo()!=null) {
            if (count != 0) {
                sql += ",";
            }
            sql += " refund_two_days_ago =" + ticket.getRefundTwoDaysAgo() + "";
            count++;
        }
        if (StringUtils.isNotBlank(ticket.getStatus())) {
            if (count != 0) {
                sql += ",";
            }
            sql += " status ='" + ticket.getStatus() + "'";
            count++;
        }
        if (ticket.getIsDeleted()!=null) {
            if (count != 0) {
                sql += ",";
            }
            sql += " is_deleted ='" + ticket.getIsDeleted() + "'";
            count++;
        }
        if (ticket.getTermOfValidityStartTime()!=null) {
            if (count != 0) {
                sql += ",";
            }
            sql += " term_of_validity_start_time ='" + DateToStringUtil.getStringDateTime(ticket.getTermOfValidityStartTime()) + "'";
            count++;
        }
        if (ticket.getTermOfValidityEndTime()!=null) {
            if (count != 0) {
                sql += ",";
            }
            sql += " term_of_validity_end_time ='" + DateToStringUtil.getStringDateTime(ticket.getTermOfValidityEndTime()) + "'";
            count++;
        }
        if (ticket.getFixedRate()!=null) {
            if (count != 0) {
                sql += ",";
            }
            sql += " fixed_rate ='" + ticket.getFixedRate() + "'";
            count++;
        }
        if (StringUtils.isNotBlank(ticket.getCrowd())) {
            if (count != 0) {
                sql += ",";
            }
            sql += " crowd ='" + ticket.getCrowd() + "'";
            count++;
        }
        if (ticket.getConsumeId()!=null) {
            if (count != 0) {
                sql += ",";
            }
            sql += " consume_id =" + ticket.getConsumeId() + "";
            count++;
        }
        sql += " where id='" + ticket.getId() + "'";
        return sql;
    }

    public String selectRejectInfoByProId(Long productId){
        String sql = " select " +
                "  ticket.reject_reason,account.account_tel,account.account_name from " +
                "  eboos_ticket ticket " +
                "  left join eboos_account account " +
                "   on ticket.reject_user = account.id" +
                " where ticket.id = '"+productId+"'  ";
        return sql;
    }


    public String findTicketByParamDealer(TicketQueryPo ticket){
        String sql = findTicketByParamDealerSql;

        if(StringUtils.isNotBlank(ticket.getName()))
            sql += " and ticket.name like '%" + ticket.getName() + "%'";
        if(ticket.getSupplierId() != null)
            sql += " and disAsso.distributor_id='" + ticket.getSupplierId() + "'";
        if(StringUtils.isNotBlank(ticket.getStatus()))
            sql += " and ticket.status='" + ticket.getStatus() + "'";
        if(ticket.getPoiId()!=null)
            sql += " and consumeRelation.poi_id ='" + ticket.getPoiId() + "'";
        sql += " GROUP BY product.id,ticket.common_product_id order by product.created_date desc";
        return sql;
    }

}
